package com.assistant.hanleTask;

import com.assistant.utils.CommonComponent;
import com.assistant.xmlParse.XmlParser;

import javax.swing.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import static com.assistant.utils.CommonDefault.getPlatformBASeparator;
import static com.assistant.utils.CommonDefault.getPlatformSeparator;

public  class HandleCommand {
    private HandleCommand() {
    }

    public static String handleCommandForMakeLogApk(String cmd, FileOutputStream fileWriter) {
        try {
            String content11 = "start command "+cmd+"\n";
            fileWriter.write(content11.getBytes());
            Runtime run = Runtime.getRuntime();
            String content = null;
            BufferedReader bufferedReader = null;
            InputStreamReader inputStreamReader;
            Process p = null;
            p = run.exec(cmd);
            inputStreamReader = new InputStreamReader(p.getInputStream(), StandardCharsets.UTF_8);
            bufferedReader = new BufferedReader(inputStreamReader);
            content = getContentLog(bufferedReader);
            content11 = "command end and the result is \n"+content+"\n";
            fileWriter.write(content11.getBytes());
            //p.waitFor();
            return content;
        } catch (IOException e) {
            try {
                String content11 = "cant do something... The cause of the problem that cmd is " + cmd + " \n";
                fileWriter.write(content11.getBytes());
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return "";
    }

    private static String getContentLog(BufferedReader bufferedReader) throws IOException {
        String line;
        String content = "";
        while ((line = bufferedReader.readLine()) != null) {
            content += line + "\n";
        }
        System.out.println(content);
        return content;
    }

    public static boolean checkIsSDKApk(String targetPath) {
        File sdkSo = new File(targetPath + getPlatformBASeparator("lib"));
        File firstFile = Objects.requireNonNull(sdkSo.listFiles())[0];
        for (String fileName : firstFile.list()) {
            if (fileName.equals("libQsu.so")) {
                return true;
            }
        }
        return false;
    }
    public static  void handleTextFieldIsEmpty(JPanel jPanel,String content) {
        CommonComponent.warningAlert(jPanel, "警告", content);
    }

    public static void setjProgressValue(JProgressBar jProgressBar,int value) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                jProgressBar.setValue(value);
            }
        });
    }
    public static void logFileIsExist(File logFile){
        if (logFile.exists()) {
            System.out.println("there is log file exist，delete。。。");
            logFile.delete();
            System.out.println("删除成功...");
            try {
                logFile.createNewFile();
            } catch (IOException e) {
                System.out.println("can't create log file...");
                e.printStackTrace();
            }
        }
    }

    public static String parseXml(String path) {
        //解析xml
        File file = new File(path + getPlatformSeparator() + "AndroidManifest.xml");
        System.out.println("解析xml路径:" + file.getAbsolutePath());
        XmlParser xmlParser = new XmlParser(file);
        String relativePath = xmlParser.parseManifest();
        xmlParser.injectDebuggable();
        System.out.println("获取到启动界面activity路径:" + relativePath);
        return relativePath;
    }

}
